package com.app.service.impl;

import cn.hutool.core.util.StrUtil;
import com.app.mapper.*;
import com.app.model.*;
import com.app.service.CategoryService;
import com.app.service.FileService;
import com.app.service.GoodsService;
import com.app.utils.StringTool;
import com.app.vo.GoodsVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品信息
 *
 * @author 16693103420
 * @since 2024-01-25
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, GoodsModel> implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsSpecsMapper goodsSpecsMapper;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private FileService fileService;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    public ProvinceMapper provinceMapper;

    //商品信息列表
    @Override
    public GoodsVo getGoodsList(Integer page, Integer limit, Integer type, String name, AdminUserModel user, Integer areaId,Integer inventory) {
        Page<GoodsModel> page1 = new Page<>(page, limit);
        QueryWrapper<GoodsModel> qw = new QueryWrapper<>();
        if (user.getRoleId() == 3) {
            qw.eq("city_id",areaId).or().eq("city_id", user.getAreaId());
        }
        if (user.getRoleId() == 4) {
            qw.eq("area_id",areaId).or().eq("area_id", user.getAreaId());
        }
        if (type != null && type != 0) {
            qw.eq("type", type);
        }
        if (StringTool.isNotEmpty(name)) {
            qw.like("name", name);
        }
        qw.eq(inventory!=null,"number",inventory);
        qw.orderByDesc("create_time");
        Page<GoodsModel> goodsPage = goodsMapper.selectPage(page1, qw);
        List<GoodsModel> goodsList = goodsPage.getRecords();
        for (GoodsModel goods : goodsList) {
            if (goods.getType() != null) {
                CategoryModel categoryModel = categoryService.getById(goods.getType());
                if (categoryModel != null) {
                    goods.setTypeText(categoryModel.getName());
                }
            }
            //构建areaText
            String areaText;
            String businessAreaId = goods.getAreaId();
            QueryWrapper<AreaModel> areaQW = new QueryWrapper<>();
            areaQW.eq("area_id", businessAreaId);
            AreaModel areaModel = areaMapper.selectOne(areaQW);
            areaText = areaModel.getArea();
            if (goods.getCityId() != null) {
                CityModel cityModel = cityMapper.selectOne(new QueryWrapper<CityModel>()
                        .eq("city_id", goods.getCityId()));
                String city = cityModel.getCity();
                ProvinceModel provinceModel = provinceMapper.selectOne(new QueryWrapper<ProvinceModel>()
                        .eq("province_id", cityModel.getProvinceId()));
                areaText = provinceModel.getProvince() +"\\" +city+"\\"+areaText;
            }
            goods.setAreaText(areaText);
        }
        long count = goodsPage.getTotal();
        GoodsVo goodsVo = new GoodsVo();
        goodsVo.setList(goodsList);
        goodsVo.setCount((int) count);
        return goodsVo;
    }

    @Override
    public GoodsModel getGoodsById(Integer id) {
        GoodsModel goods = goodsMapper.selectById(id);
        List<FileModel> fileModelList = new ArrayList<>();
        if (StrUtil.isNotBlank(goods.getFileId())) {
            String[] split = goods.getFileId().split(",");
            for (String s : split) {
                FileModel fileModel = fileService.getById(s);
                fileModelList.add(fileModel);
            }
            goods.setFile(fileModelList);
        }
        return goods;
    }

    @Override
    public void delGoods(Integer id) {
        QueryWrapper<GoodsSpecsModel> qw = new QueryWrapper<>();
        qw.eq("goods_id", id);
        goodsSpecsMapper.delete(qw);
        goodsMapper.deleteById(id);
    }

    @Override
    public GoodsVo getUserGoodsList(Integer page, Integer limit, Integer type, Integer applyType, String name, HttpServletRequest request,Integer sort) {
        Page<GoodsModel> page1 = new Page<>(page, limit);
        QueryWrapper<GoodsModel> qw = new QueryWrapper<>();
        if (type != null && type != 0) {
            qw.eq("type", type);
        }
        qw.eq(applyType!=null, "apply_type",applyType);
        if (StringTool.isNotEmpty(name)) {
            qw.like("name", name);
        }
        qw.orderByAsc("sort");
        if(sort!=null)
        {
            switch (sort)
            {
                case 1:qw.orderByDesc("integral,voucher");break;
                case 2:qw.orderByDesc("create_time");
            }
        }

        Page<GoodsModel> goodsPage = goodsMapper.selectPage(page1, qw);
        List<GoodsModel> goodsList = goodsPage.getRecords();
        for (GoodsModel goods : goodsList) {
            if (goods.getType() != null) {
                CategoryModel categoryModel = categoryService.getById(goods.getType());
                if (categoryModel != null) {
                    goods.setTypeText(categoryModel.getName());
                }
            }
        }
        long count = goodsPage.getTotal();
        GoodsVo goodsVo = new GoodsVo();
        goodsVo.setList(goodsList);
        goodsVo.setCount((int) count);
        return goodsVo;
    }
}
